﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;

namespace App.Bus
{
	/// <summary>
	/// 类Consume。
	/// </summary>
	[Serializable]
	public partial class Consume
	{
		public Consume()
		{}
		#region Model
		private int _id;
		private int _accountid;
		private DateTime _insertdatetime;
        private int _cost;
		private string _imagepath;
		/// <summary>
		/// 
		/// </summary>
		public int Id
		{
			set{ _id=value;}
			get{return _id;}
		}
		/// <summary>
		/// 
		/// </summary>
		public int AccountId
		{
			set{ _accountid=value;}
			get{return _accountid;}
		}
		/// <summary>
		/// 
		/// </summary>
		public DateTime InsertDateTime
		{
			set{ _insertdatetime=value;}
			get{return _insertdatetime;}
		}
		/// <summary>
		/// 
		/// </summary>
        public int Cost
		{
			set{ _cost=value;}
			get{return _cost;}
		}
		/// <summary>
		/// 
		/// </summary>
		public string ImagePath
		{
			set{ _imagepath=value;}
			get{return _imagepath;}
		}
		#endregion Model


		#region  Method

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Consume(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,AccountId,InsertDateTime,Cost,ImagePath ");
			strSql.Append(" FROM [Consume] ");
			strSql.Append(" where Id=@Id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
			parameters[0].Value = Id;

			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					this.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["AccountId"]!=null && ds.Tables[0].Rows[0]["AccountId"].ToString()!="")
				{
					this.AccountId=int.Parse(ds.Tables[0].Rows[0]["AccountId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["InsertDateTime"]!=null && ds.Tables[0].Rows[0]["InsertDateTime"].ToString()!="")
				{
					this.InsertDateTime=DateTime.Parse(ds.Tables[0].Rows[0]["InsertDateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Cost"]!=null && ds.Tables[0].Rows[0]["Cost"].ToString()!="")
				{
					this.Cost=int.Parse(ds.Tables[0].Rows[0]["Cost"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ImagePath"]!=null && ds.Tables[0].Rows[0]["ImagePath"].ToString()!="")
				{
					this.ImagePath=ds.Tables[0].Rows[0]["ImagePath"].ToString();
				}
			}
		}

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{

		return DbHelperSQLite.GetMaxID("Id", "Consume"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from [Consume]");
			strSql.Append(" where Id=@Id ");

			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
			parameters[0].Value = Id;

			return DbHelperSQLite.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into [Consume] (");
			strSql.Append("AccountId,InsertDateTime,Cost,ImagePath)");
			strSql.Append(" values (");
			strSql.Append("@AccountId,@InsertDateTime,@Cost,@ImagePath)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@AccountId", DbType.Int32,8),
					new SQLiteParameter("@InsertDateTime", DbType.DateTime),
					new SQLiteParameter("@Cost", DbType.Int32,8),
					new SQLiteParameter("@ImagePath", DbType.String,100)};
			parameters[0].Value = AccountId;
			parameters[1].Value = InsertDateTime;
			parameters[2].Value = Cost;
			parameters[3].Value = ImagePath;

			DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update()
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update [Consume] set ");
			strSql.Append("AccountId=@AccountId,");
			strSql.Append("InsertDateTime=@InsertDateTime,");
			strSql.Append("Cost=@Cost,");
			strSql.Append("ImagePath=@ImagePath");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@AccountId", DbType.Int32,8),
					new SQLiteParameter("@InsertDateTime", DbType.DateTime),
					new SQLiteParameter("@Cost", DbType.Int32,8),
					new SQLiteParameter("@ImagePath", DbType.String,100),
					new SQLiteParameter("@Id", DbType.Int32,8)};
			parameters[0].Value = AccountId;
			parameters[1].Value = InsertDateTime;
			parameters[2].Value = Cost;
			parameters[3].Value = ImagePath;
			parameters[4].Value = Id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from [Consume] ");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
			parameters[0].Value = Id;

			int rows=DbHelperSQLite.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public void GetModel(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,AccountId,InsertDateTime,Cost,ImagePath ");
			strSql.Append(" FROM [Consume] ");
			strSql.Append(" where Id=@Id ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@Id", DbType.Int32,4)};
			parameters[0].Value = Id;

			DataSet ds=DbHelperSQLite.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					this.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["AccountId"]!=null && ds.Tables[0].Rows[0]["AccountId"].ToString()!="")
				{
					this.AccountId=int.Parse(ds.Tables[0].Rows[0]["AccountId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["InsertDateTime"]!=null && ds.Tables[0].Rows[0]["InsertDateTime"].ToString()!="")
				{
					this.InsertDateTime=DateTime.Parse(ds.Tables[0].Rows[0]["InsertDateTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Cost"]!=null && ds.Tables[0].Rows[0]["Cost"].ToString()!="")
				{
					this.Cost=int.Parse(ds.Tables[0].Rows[0]["Cost"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ImagePath"]!=null && ds.Tables[0].Rows[0]["ImagePath"].ToString()!="")
				{
					this.ImagePath=ds.Tables[0].Rows[0]["ImagePath"].ToString();
				}
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM [Consume] ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQLite.Query(strSql.ToString());
		}

		#endregion  Method
	}
}

